package com.cloud.im.demo;

import com.cloud.easy.im.BaseServerApplication;
import com.cloud.easy.im.argeement.Agreement;
import com.cloud.easy.im.event.MessageQoSEventListenerS2C;
import com.cloud.easy.im.event.ServerEventListener;
import com.cloud.easy.im.handler.ServerWebSocketCoreHandler;
import io.netty.channel.Channel;
import io.netty.handler.logging.LogLevel;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangjian
 * @version 0.1.0
 * @create 2020年09月03日11:49 上午
 */
public class ServerApplicationImpl {

    public static void main(String[] args) throws Exception {
        // 实例化后记得startup哦，单独startup()的目的是让调用者可以延迟决定何时真正启动IM服务
        BaseServerApplication build = new BaseServerApplication.Builder()
                .setServerEventListener(new ServerEventListener() {
                    @Override
                    public byte onVerifyUser_CallBack(String userId, String token, String extra, Channel session) {
                        return 0;
                    }

                    @Override
                    public void onUserLoginAction_CallBack(String userId, String extra, Channel session) {

                    }

                    @Override
                    public void messageBefore_CallBack(Agreement dataFromClient) {

                    }

                    @Override
                    public void messageSendSuccess_C2C_CallBack(Agreement dataFromClient) {

                    }

                    @Override
                    public boolean messageSendFaild_C2C_CallBack(Agreement dataFromClient) {
                        return false;
                    }

                    @Override
                    public void onUserLogoutAction_CallBack(String userId, Object obj, Channel session) {

                    }

                    @Override
                    public List<String> messageGroupAction_CallBack(String groupId, String userId) {
                        return null;
                    }
                })
                .setMessageQoSEventListenerS2C(new MessageQoSEventListenerS2C(){

                    @Override
                    public void messagesLost(ArrayList<Agreement> lostMessages) {

                    }

                    @Override
                    public void messagesBeReceived(String theFingerPrint) {

                    }
                })
                .setAbstractServerHandler(new ServerWebSocketCoreHandler())
                .setLogLevel(LogLevel.INFO)
                .setRunRedis(false)
                .setPort(7788)
                .build();
        // 启动IMSDK服务端的Demo
        build.startUp();

        // 加一个钩子，确保在JVM退出时释放netty的资源
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                build.shutdown();
            }
        });
    }

}
